<?php
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */
/**
 * Class to manipulate Images.
 * @author frederic
 *
 */
class Image{
	protected static $thumbSizes=array('micro'=>40, 'mini'=>80, 'medium'=>160, 'full'=>320);

	/**
	 * resize the $fullpath image to the $width x $height or $size.
	 * @param $fullpath path to the image to be resized.
	 * @param $pWith new width of the image. 
	 * @param $pHeight ne height of the image.
	 * @param $pSize ne size of the image (FULL, MEDIUM, MINI, MICRO).
	 * @return String containing the full path to the new resized image.
	 */
	public function resizeImage($fullpath="",$pWidth=0,$pHeight=0,$pSize="medium",$options=array()){
		__debug(__CLASS__."::resizeImage() - start : fullpath=$fullpath,pWidth=$pWidth,pHeight=$pHeight,pSize=$pSize",__FILE__,__LINE__);
		if($fullpath=="" || !file_exists($fullpath)) {
			__error(__CLASS__."::resizeImage() - parameters not correctly initialized.");
			return "error";
		}
		$imagePath="";
		//read information about file.
		$info = pathinfo($fullpath);
		__debug(__CLASS__."::resizeImage() - fullpath=[".print_r($info,true)."]",__FILE__,__LINE__);
		
		//depends of extension of image
		switch($info['extension']){
			case "jpg":
			default:
				$image=imagecreatefromjpeg($fullpath);
				break;
			case "png":
				$image=imagecreatefrompng($fullpath);
				break;
			case "gif":
				$image=imagecreatefromgif($fullpath);
				break;
		}
		//determine the new size of the image
		if(isset($image)){
	      $width = imagesx( $image );
	      $height = imagesy( $image );
	      __debug(__CLASS__."::resizeImage() - image width=$width, height=$height",__FILE__,__LINE__);
	      
	      // calculate thumbnail size

	      if($pSize!=""){
	      	switch($pSize){
	      		case "micro":
	      			$pWidth=40;
	      			break;
	      		case "mini":
	      			$pWidth=80;
	      			break;
            	case "medium":
            		$pWidth=160;
            		break;
            	case "full":
	      			$pWidth=320;
            		break;
	      	}
	      }
          if($pWidth!=0){
		      	$new_width = $pWidth;
		      	$new_height = floor( $height * ( $pWidth / $width ) );
	      }else if ($pHeight!=0){
	      	$new_height = $pHeight;
	      	$new_width = floor( $width * ( $pHeight / $height ) );
	      	$new_height = floor( $height * ( $pWidth / $width ) );
	      }
	      $dirpath=dirname($_SERVER['SCRIPT_FILENAME'])."/".$info['dirname']."/".($pSize!=""?$pSize:$new_height."x".$new_width);
	      __debug(__CLASS__."resizeImage() - dirpath=[$dirpath]",__FILE__,__LINE__);
		  if(!file_exists($dirpath)){
		  	mkdir($dirpath,0777);
		  }
          $imagePath = $info['dirname']."/".($pSize!=""?$pSize:$new_height."x".$new_width)."/".$info['filename'].".".$info['extension'];
	      if(!file_exists($imagePath)){
	      	__debug(__CLASS__."::resizeImage() - image resized to (width=$new_width, height=$new_height)",__FILE__,__LINE__);
	      	// create a new temporary image
		      $tmp_img = imagecreatetruecolor( $new_width, $new_height );
		      // copy and resize old image into new image
		      imagecopyresampled( $tmp_img, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
		
		      // save thumbnail into a file
		      imagejpeg( $tmp_img, $imagePath );
	      }else{
	      	__debug(__CLASS__."::resizeImage() - image resized already exists.",__FILE__,__LINE__);
	      }
		}
		__debug(__CLASS__."::resizeImage() - end : return=$imagePath",__FILE__,__LINE__);
		return $imagePath;
	}
	
	/**
	 * create a thumb of the image.
	 * @param $fullPathInput
	 * @param $fullPathThumbOutput
	 * @param $options
	 * @return unknown_type
	 */
	public function makeThumb($fullPathInput="",$fullPathThumbOutput="",$options=array()){
		if($fullPathInput=="" || $fullPathThumbOutput=="") {
			__error(__CLASS__."::makeThumb() - Parameters not correctly initialized.");
			return "error";
		}
		return $imagePath;
	}
	/**
	 * Add a new size to the Thumbnailer. 
	 * @param $name identifier fo this size.
	 * @param $width width of the thumbnail.
	 * @return unknown_type
	 */
	public function addThumbSize($name,$width){
		self::$thumbSizes[$name]=$width;
	}
	
}
?>